package com.TheyChat.service.config.core.entity;

import cn.hutool.core.util.StrUtil;
import com.TheyChat.service.config.core.exception.BusinessException;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author: victor.w.ye
 * @Date: 2021/2/20 15:32
 * @Copyright:
 * @Company: luban
 * @Version: 1.0.0
 **/
@Data
public class ResponseMessage<T> implements Serializable {

    /**
     * 状态码
     */
    private int code;

    /**
     * 状态消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;


    public ResponseMessage() {
        super();
    }

    public ResponseMessage(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseMessage(int code, String message, boolean ok) {
        this.code = code;
        this.message = message;

    }

    public ResponseMessage(int code, String message, boolean ok, T data) {
        this.code = code;
        this.message = message;
        this.data = data;

    }

    public T fetch() {
        if (this.code != 0) {
            String err = StrUtil.isNotBlank(this.message) ? this.message : "远程调用结果异常";
            throw new BusinessException(err);
        }

        return getData();
    }

}
